package org.grow.sha.service;

import org.grow.sha.dao.CoreTask;
import org.grow.sha.dao.EggTimeoutException;
import org.grow.sha.entity.Account;
import org.grow.sha.serviceAop.Modal;
import org.grow.sha.serviceAop.Tab;
import org.grow.sha.serviceAop.ValidatePosition;
import org.grow.sha.tool.RobotEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;

import static org.grow.sha.service.TaskExeService.corePoint;

@Component
public class GreenActivity {
    @Autowired
    private RobotEx robot;
    Logger logger = LoggerFactory.getLogger(GreenActivity.class);

    @Autowired
    private CoreTask coreTask;

    public static final Color purpleEggColor1 = new Color(185,155,93);
    @Value("${error.shotsdir}")
    private String errorPicDir;
    @Tab
    @ValidatePosition
    public void purpleEgg(Account account,boolean update,boolean sweep){
//        按钮　逐鹿天下
        robot.moveAndClick(930, 550);

        robot.delay(2000);
//        准确定位英雄模式　第一模块　第一阶段rr
        robot.moveAndClick(717, 184);
        robot.moveAndClick(304, 366);
        robot.moveAndClick(514, 365);
//        判断是否需要收取扫荡战果
//         [x=382,y=326]
//java.awt.Color[r=189,g=2,b=2]
//java.awt.Color[r=193,g=2,b=2]
//        Color p = robot.getPixelColor(384, 328);
//        if (robot.closeTo(p, purpleEggColor1)) {
//            robot.moveAndClick(1214, 188);
//            robot.click();
//        }
//        以上判断容易被祈福弹窗盖住
//        1213 190 new Color(185,155,93)
        Color p = robot.getPixelColor(1213, 190);
        if (robot.closeTo(p,purpleEggColor1)){
            robot.moveAndClick(1213,190);
            robot.click();
        }


//先购买一个体力 r=42,g=133,b=32
        robot.moveAndClick(1059, 677);
// 确定按钮
        robot.moveAndClick(710, 591);
//        空闲点
        robot.moveAndClick(1006,474);
// 核心刷逐鹿
//        this.coreEgg(username,20);
//        最多刷20

        for (int i = 0; i < 20; i++) {
            boolean b;
            try {
                if (update){
                    b= this.coreTask.eggUpdate();
                }else {

                    b = this.coreTask.eggBattle();
                }
                if (!b) {
                    logger.debug("逐鹿体力已经不足，今日" + account.getUsername() + "已经刷了" + i + "个体力");
                    logger.info(account.getUsername() + "已经刷了" + i + "个逐鹿体力");
                    break;
                }
            } catch (EggTimeoutException e) {
                String name = System.currentTimeMillis()+"-PurpleEgg.png";
                logger.warn( account.getUsername()+ "已经刷了" + (i + 1) + "个逐鹿体力,出了超时异常,尝试进行下一次 "
                        +e.getMessage()+" 排查文件: "+name);
                try {
                    robot.createErrorSnapshot(new File(errorPicDir+"/"+name));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
               continue;
            }

        }

//    刷完后可以添加一个扫荡环节

        if (sweep) {
//            扫荡按钮
            robot.moveAndClick(1220, 510);
//            添加将灵按钮
            robot.moveAndClick(904, 458);
            robot.delay(1000);
//            选择第一个将灵
            robot.moveAndClick(522, 393);
//          点击确定
            robot.moveAndClick(754, 634);
            robot.delay(1000);
//            开始扫荡
            robot.moveAndClick(944, 663);
//            点击关闭界面
            robot.moveAndClick(815, 635);
        }



    }


    //    r=186,g=2,b=2  1084 660
    public static final Color sprintColor1 = new Color(186, 2, 2);
    public static final Color sprintColor2 = new Color(47,32,27);
    //    　882　614 ===>>>　240 189 95
//    253 225 109?
    public static final Color sprintColor3 = new Color(253, 225, 109);
    //    出征有限
    public static final Color sprintColor64 = new Color(93, 162, 90);
    public static final Color sprintColor63 = new Color(137, 55, 181);
    public static final Color sprintColor62 = new Color(217, 148, 13);
    public static final Color sprintColor61 = new Color(212, 30, 29);
    @Tab
    @ValidatePosition
    public boolean sprintTask(Account account) {
//24 :  [x=360,y=666]

//java.awt.Color[r=48,g=32,b=26]  //java.awt.Color[r=47,g=32,b=27]
//        判断将灵在不在起始位置　如果不在　去另一个页面然后返回
        robot.delay(1000);
        if (robot.closeTo(robot.getPixelColor(360,666),sprintColor2)) {
            robot.moveAndClick(1170, 585);
            robot.delay(2000);
            robot.moveAndClick(corePoint.x, corePoint.y);
            robot.delay(1000);
        }
        robot.moveAndClick(360, 666);
        robot.delay(2000);
//        聚宝盆　领取
        if (robot.getPixelColor(1084, 660).equals(sprintColor1)) {
//            打开聚宝盆
            robot.moveAndClick(1084, 660);
//            领取
            robot.moveAndClick(867, 542);
            logger.info(account.getUsername() + "聚宝盆收获一次");
            robot.click();
//            关闭
            robot.moveAndClick(970, 329);

        }
//        将灵自动加速 一小时内判定
//        出征领取设置
        if (robot.getPixelColor(1164, 660).equals(sprintColor1)) {
            robot.moveAndClick(1164, 660);
            robot.delay(1000);
            robot.moveAndClick(760, 633);
//            领取奖励按钮
//            奖励列表　刷新完　等待一个确定按钮
//            　882　614 ===>>>　240 189 95
//            this.waitAppearAndClick(882, 614,sprintColor3);
            robot.waitThenClick(882, 614, sprintColor3);
//选择出征将灵

            robot.moveAndClick(578, 642);
            robot.delay(1000);
//选择出征任务
//            观测点　任字 单人旁下　1064 380 511 642  +=31
//            四级任务绿色　r=93,g=162,b=90
//            三级任务紫色  r=69,g=30,b=78 r=71,g=34,b=81
//            二级任务黄色　r=217,g=148,b=13
//            一级任务红色　r=212,g=30,b=29
//            三个点选一个,
            int[] tasksPriority = new int[]{4, 3, 2, 1};
            String s = this.chooseTask(tasksPriority);

//            点击出征
            robot.moveAndClick(547, 645);
//          点击确定出征
            robot.moveAndClick(667, 533);
            String t = account.getUsername() + "将灵出征一次, " + s;
            logger.info(t);
//            点击关闭出征页面
            robot.moveAndClick(1145, 235);
        }
//        返回主页面
//        robot.moveAndClick(corePoint.x, corePoint.y);
//        backToMainMenu();
        return false;
    }
    public static final Color[] sprintColor6s = new Color[]{sprintColor61, sprintColor62, sprintColor63, sprintColor64};

    public String chooseTask(int[] tasksPriority) {
        //选择出征任务
//        备用观测点　1108 367 500 631
//            观测点　任字 单人旁下　1064 380 511 642  +=131
//            四级任务绿色　r=93,g=162,b=90
//            三级任务紫色  137, 55, 181
//            二级任务黄色　r=217,g=148,b=13
//            一级任务红色　r=212,g=30,b=29
//            三个点选一个,
        int y = 380;
        final int d = 131;
        Color[] tasks = new Color[3];
        for (int i = 0; i < tasks.length; i++) {
            tasks[i] = robot.getPixelColor(1064, 380 + i * d);
        }
        System.out.println(Arrays.toString(tasks));
//        int[] tasksPriority= new int[]{4,3,2,1};
//   a +郭嘉    int[] tasksPriority= new int[]{3,4,2,1};
//   sa     int[] tasksPriority= new int[]{2,3,4,1};
//
//
        boolean found = false;
        int position = 0;
        int level = -1;

        for (int j = 0; j < tasksPriority.length; j++) {
            if (!found) {
                for (int i = 0; i < tasks.length; i++) {
                    double r = robot.distance(tasks[i], sprintColor6s[tasksPriority[j] - 1]);
//                    System.out.println("颜色距离　待测试　第" + i + " 个点:: == >>>> " + r);
                    if (r < 0.3) {
                        found = true;
                        position = i;
                        level = tasksPriority[j];
                        y = y + i * d;

                        break;
                    }
                }
                if (!found) {
                    System.out.println(String.format("没有%d级任务", 4 - j));
                }
            } else {
                break;
            }
        }
        System.out.println("最终敲定出征第几个？　380 511 642 ? " + found + " == " + y);

        robot.moveAndClick(1064, y);
        return String.format("第%d位置的%d级出征", position, level);

    }
    public final static Color dailySignColor1 = new Color(172, 134, 86);
    public final static Color dailySignColor2 = new Color(200, 2, 2);
    public final static Point dailySignPoint1=new Point(283, 289);
    public final static Point dailySignPoint2=new Point(283, 345);


    @Modal
    @ValidatePosition
    public void dailySign(Account account,int point){

//[x=308,y=274]
//java.awt.Color[r=206,g=1,b=1]
//        [x=309,y=380]
//java.awt.Color[r=187,g=2,b=2]
// 274 326 378 430 482 +=52
//        robot.delay(1000);
//        点击右下　活动按钮
        robot.moveAndClick(1130, 740);
        robot.delay(2000);
//        可能出现新活动小窗口1043 281  172 134 86
        if (robot.getPixelColor(1043, 281).equals(dailySignColor1)) {
            robot.moveAndClick(1043, 281);
        }
        LocalDate now = LocalDate.now();
        int dayOfMonth = now.getDayOfMonth();
//        点击每日签到
//        robot.moveAndClick(287,293);
//        int dayOfWeek = now.getDayOfWeek().getValue();
//        智能签到
        Point p =dailySignPoint2;
        if (point == 10){
            if (robot.closeTo(
                    robot.getPixelColor(309,274),
                    dailySignColor2)){

//                p=new Point(283, 289);
                p=dailySignPoint1;
            }else if (robot.closeTo(
                    robot.getPixelColor(309,326),
                    dailySignColor2)){
                robot.moveAndClick(309,326);
                p=dailySignPoint2;
            }
        }
        if(point == 1){
           p=dailySignPoint1;
        }
        if (point == 2){
            p=dailySignPoint2;
//           p=new Point(283, 345);
        }
//        if (dayOfWeek == 5) {
//            robot.moveAndClick(283, 289);
//        } else {
//
//            robot.moveAndClick(283, 345);
//        }

//        int shiftW =  (dayOfMonth-1) % 5 ;
        robot.moveAndClick(p.x,p.y);
        int ml = now.lengthOfMonth();
        if (ml == 31) {
            int shiftH = 474;
//        526 606 686
            if (dayOfMonth > 20) {
                shiftH = ((dayOfMonth - 20 - 1) / 5) * 80 + 526;
            }

            int shiftW = (dayOfMonth - 1) % 5 * 92 + 440;
//        20天以后　下拉框的ｙ值不会是一成不变的了
            logger.debug("今天是本月第　" + dayOfMonth + " 天　　=== 左右 " + shiftW + " ===上下　" + shiftH
            );
// 绿色点        478 460  x += 99
// 领奖点        478 　486  x += 99
//        改版　440 532 624 716  +=92    : 474
            robot.mouseMove(shiftW, shiftH);
            robot.moveAndClick(shiftW, shiftH);

        }else {
//            30天的解决方案
            int shiftH = 474;
//        522 602 682
            if (dayOfMonth > 15) {
                shiftH = ((dayOfMonth - 15 - 1) / 5) * 80 + 524;
            }

            int shiftW = (dayOfMonth - 1) % 5 * 92 + 440;
//        20天以后　下拉框的ｙ值不会是一成不变的了
            logger.debug("今天是本月第　" + dayOfMonth + " 天　　=== 左右 " + shiftW + " ===上下　" + shiftH
            );
// 绿色点        478 460  x += 99
// 领奖点        478 　486  x += 99
//        改版　440 532 624 716  +=92    : 528
            robot.mouseMove(shiftW, shiftH);
            robot.moveAndClick(shiftW, shiftH);
        }

        robot.click();
        logger.info(account.getUsername() + "签到一次　" + dayOfMonth);
        robot.moveAndClick(1171, 228);

    }

    public static final Color rewardColor1 = new Color(56, 41, 9);
    @Modal
    @ValidatePosition
    public void getReward(Account account) {
        robot.moveAndClick(1226, 663);
        robot.moveAndClick(434, 334);

        robot.mouseMove(1042, 380);
        robot.delay(1000);
//     已经作废 可以领取金黄色  r=255,g=243,b=134　不可以领取灰色[r=207,g=192,b=162　
//        [x=1042,y=380]　可以领取
//java.awt.Color[r=56,g=41,b=9]　１５８　１３６　５４　不可领取　
        int l = 0;
        for (; ; ) {
            if (robot.closeTo(robot.getPixelColor(1042, 380), rewardColor1)) {
                robot.click();
                l++;
            } else {
                break;
            }
        }
//        if (i >= 2) robot.moveAndClick(702, 284);
        if (l > 10) l = 10;
//       普通将印宝箱没有活跃度　需要少一次，
//        l--;
        for (int k = 0; k < (l / 2); k++) {
            robot.moveAndClick(702 + k * 90, 284);
        }
        logger.info(account.getUsername() + "自动领取了" + l + "个每日小奖励和" + l / 2 + "个每日大奖励");
//        关闭领奖页面
        robot.moveAndClick(1143, 231);

    }

}
